function makeEl( el ) { return $(document.createElement(el)); }

function getCurrentDateTime() {

    var dateTimeString = new StringBuffer();

    var currentDate = new Date()

    dateTimeString.append( currentDate.getDate() );
    var minutes = currentDate.getMinutes();
    if( minutes < 10 ) { minutes = "0" + minutes; }

    return currentDate.getDate() + "/" + (currentDate.getMonth()+1) + "/" + currentDate.getFullYear() + " " + currentDate.getHours() + ":" + minutes;
}

function StringBuffer() { 
   this.buffer = []; 
} 

StringBuffer.prototype.append = function append(string) { 
    this.buffer.push(string); 
    return this; 
}; 

StringBuffer.prototype.toString = function toString() { 
    return this.buffer.join(""); 
};

function addShadowboxKeyPressHandlers() {

    $(document).bind('keydown', shadowboxKeyPress );

}

function shadowboxKeyPress( e ) {
    if(e.keyCode==27 && Shadowbox ) { Shadowbox.close(); }
    if(e.keyCode==13 && Shadowbox ) { $('#shadowboxSubmitButton').click(); }
}

function closeShadowbox() { Shadowbox.close(); }

// taken from the internet to determine the width of the browser scrollbar
function getScrollBarWidth() {
    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
    // Append our div, do our calculation and then remove it
    $('body').append(div);
    var w1 = $('div', div).innerWidth();
    div.css('overflow-y', 'scroll');
    var w2 = $('div', div).innerWidth();
    $(div).remove();
    return (w1 - w2);
}

function setGlobals( p ) {

    var localGlobal = globals;

    for( var key in p ) {
        localGlobal[key] = p[key];
    }
}


function addContextHighlight() { $(this).addClass('contextHighlight'); }
function removeContextHighlight() { $(this).removeClass('contextHighlight'); }
function closeContextMenu( p ) { p.data.empty().remove(); }

function isContextMenu() {
    
    var contextMenu = $('#contextMenu');

    if( contextMenu.length ) { return contextMenu; }
    else { return false; }
}

function makeContextMenu( p ) {

    var contextMenu = makeEl('ul').attr( { 'id': 'contextMenu', 'class': 'contextMenu' } )
                                  .css( { left: p.x + 2, top: p.y + 2 } );

    for( var i = 0, ii = p.menuItems.length; i < ii; i++ ) {
    
        contextMenu.append( makeEl( 'li' ).attr( { 'class': 'bottomBorder padding5p' } )
                                          .mouseover( addContextHighlight )
                                          .mouseout( removeContextHighlight )
                                          .bind( 'click', p.menuItems[i].params, p.menuItems[i].func )
                                          .bind( 'click', contextMenu, closeContextMenu )
                                          .text( p.menuItems[i].text ) );
    }

    p.container.append( contextMenu );
}

function makeAjaxURL( p ) {
    var url = new StringBuffer();
    
    url.append('/').append(globals.applicationName).append('/default');
    
    for( var i = 0, ii = p.argList.length; i < ii; i++ ) { url.append('/').append( p.argList[i] ); }

    return url;
}

function mouseOnElement( p ) {

    var elementOffset = p.el.offset();

    if( p.x < elementOffset.left ||
        p.y < elementOffset.top ||
        p.x > ( elementOffset.left + p.el.width() ) ||
        p.y > ( elementOffset.top + p.el.height() ) ) { 

            return false;

    } else {

        return true;
    }
}

function ask( p ) {

    var askContainer = makeEl('div').attr( { 'class': 'absoluteFront notifyBackground center padding5p', 'id': 'askPanel' } ).corner().append(
        makeEl('span').attr( { 'class': 'askItem' } ).text( p.text ),
        makeEl('a').attr( { 'class': 'askItem', 'href': 'javascript:void(0);' } ).text('Yes')
                   .bind( 'click', p.yesFun )
                   .bind( 'click', closeAskContainer ),
        makeEl('a').attr( { 'class': 'askItem', 'href': 'javascript:void(0);' } ).text('No')
                   .bind( 'click', p.noFun )
                   .bind( 'click', closeAskContainer ) );
    
    p.container.append( askContainer );

    var containerOffset = p.container.offset();

    askContainer.css( { 'top': containerOffset.top + 80,
                        'left': containerOffset.left + ( p.container.width() / 2 ) - ( askContainer.width() / 2 ) } );

}

function closeAskContainer() { $('#askPanel').empty().remove(); }

function notify( p ) {

    var notifyContainer = makeEl('div').attr( { 'class': 'absoluteFront notifyBackground center padding5p', 'id': 'notificationPanel' } )
                                       .text( p.text ).corner();

    p.container.append( notifyContainer );

    var containerOffset = p.container.offset();

    notifyContainer.css( { 'top': containerOffset.top + 10,
                           'left': containerOffset.left + ( p.container.width() / 2 ) - ( notifyContainer.width() / 2 ) } );

    setTimeout( closeNotify, 5000, { container: notifyContainer } );
}

function closeNotify( p ) { p.container.empty().remove(); }
